package com.cqs.leetcode;

/**
 * Created by cqs on 2018/3/24.
 */
public class SearchInsertPosition {

    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid = 0;
        while (left <= right) {
            mid = (left + right) / 2;
            if (nums[mid] == target) return mid;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;

            }
        }
        return target > nums[mid] ? mid + 1 : mid;
    }

    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        SearchInsertPosition s = new SearchInsertPosition();
        System.out.println(s.searchInsert(nums, 5) == 2);
        System.out.println(s.searchInsert(nums, 2) == 1);
        System.out.println(s.searchInsert(nums, 7) == 4);
        System.out.println(s.searchInsert(nums, 0) == 0);
    }
}
